// ---------------------------------------------------------------------------------------------------------------------------------
// PC Screen Control File for PCMod
// [CLIENTSIDE]
// Revision: 1
// ---------------------------------------------------------------------------------------------------------------------------------

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_openscreen: UserMessage Hook: Shows the PC Screen upon server notification
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_openscreen( um )
	if (!C_PRO) then C_PRO = {} end
	
	// We must assume we are opening a different PC up, so we must reset our current one
	if (C_PRO.Exists) then pc_progunload() end
	C_PC.Files = {}
	
	// Ensure the start menu is unloaded
	PCMod.StartMenu:SetV( false )
	PCMod.ProgramList:SetV( false )
	
	// Called when server tells client to open a computer screen (on pcmod_pc:Use)
	DInterface:ShowWindow( "host" )
	CurrentPCIndex = um:ReadShort()
	CurrentProgram = um:ReadString()
	CurrentWindow = um:ReadString()
	CurrentTheme = ThemeList[ um:ReadString() ]
	if (!CurrentTheme) then CurrentTheme = {} end
	CurrentPC = ents.GetByIndex( CurrentPCIndex )
	
	// Rebuild all desktop icons
	pc_builddesktopicons()
	
	// Pass focus to the icons panel
	// local dicons = DInterface:GetPanel( "dicons" )
	// if (dicons) then dicons:RequestFocus() end
	pc_focusdesktop()
	
	// Load the program if we need to
	if (CurrentProgram != "") then
		C_PRO = ProgramList[ CurrentProgram ]
		if (!C_PRO) then
			Msg( "Warning: Could not find program '" .. CurrentProgram .. "'! (No C_PRO)\n" )
			LocalPlayer():PrintMessage( HUD_PRINTTALK, "> Program failed to load client-side! (" .. CurrentProgram .. ") <" )
		else
			if (C_PRO.Exists) then
				C_PRO:ClearWindow( PTC("window") )
				C_PRO:BuildWindow( CurrentWindow, PTC("window"), DInterface["d_prop_l" .. PTC("window")] )
				pc_focusprogram()
			else
				Msg( "Warning: Could not find program '" .. CurrentProgram .. "'! (No C_PRO.Exists)\n" )
				LocalPlayer():PrintMessage( HUD_PRINTTALK, "> Program failed to load client-side! (" .. CurrentProgram .. ") <" )
			end
		end
	end
	
	// Delete the password stuff if it exists
	local txtPass = DInterface:GetObject( "txtPass", "tb", PTC("desktop") )
	local btnGo = DInterface:GetObject( "btnGo", "b", PTC("desktop") )
	if (txtPass) then DInterface:RemoveTextbox( "txtPass", PTC("desktop") ) end
	if (btnGo) then DInterface:RemoveButton( "btnGo", PTC("desktop") ) end
	
	// Set a global
	PCLoaded = true
end
usermessage.Hook( "_pcmod_openscreen", pc_openscreen )

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_closescreen: Closes the current PC's screen
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_closescreen()
	DInterface:HideWindow( "host" )
	PCLoaded = false
	//gui.EnableScreenClicker( false )
end

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_calcscreensize: Calculates the size of the screen
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_calcscreensize(hw, hh)
	local dw = (hw * 0.75)
	local dh = (hh * 0.7)
	local dx = (hw * 0.125)
	local dy = (hh * 0.125)
	if (!ShouldDrawFrame) then
		dw = (hw * 0.94)
		dh = (hh * 0.85)
		dx = (hw * 0.02)
		dy = (hh * 0.02)
	end
	return {dw, dh, dx, dy}
end

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_rebuildscreensize: Rebuilds the screen size, from the calculated screen size
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_rebuildscreensize(hw, hh)
	obj = DInterface:GetPanel( "desktop" )
	if (!obj) then return end
	ss = pc_calcscreensize( hw, hh )
	local dw = ss[1]
	local dh = ss[2]
	local dx = ss[3]
	local dy = ss[4]
	obj:SetSize(dw, dh)
	obj:SetPos(dx, dy)
	obj = DInterface:GetPanel( "window" )
	if (!obj) then return end
	obj:SetSize(dw-10, dh-50)
	pc_progunload()
end

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_rebuildscreen: Rebuilds the screen
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_rebuildscreen()
	ShouldDrawFrame = true
	if (CV_SDF:GetInt() == 0) then ShouldDrawFrame = false end
	pc_rebuildscreensize( PC_ScreenSizeX, PC_ScreenSizeY )
end
concommand.Add( "pc_rebuildscreen", pc_rebuildscreen )

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_systray_commit: Ensures an icon is on the system tray
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_systray_commit( icon )
	for k, v in pairs( C_PC.SysTray ) do
		if (v == icon) then return end
	end
	table.insert( C_PC.SysTray, icon )
	local slot = #C_PC.SysTray
	local di = vgui.Create( "Icon" )
	di:SetParent( PCMod.LowerBar )
	di:SetSize( 20, 20 )
	di:SetPos( PCMod.LowerBar:GetWide()-(slot*20), 0 )
	di:SetIcon( icon )
	C_PC.DST[ slot ] = di
end

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_systray_unlink: Ensures an icon is not on the system tray
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_systray_unlink( icon )
	for k, v in pairs( C_PC.SysTray ) do
		if (v == icon) then
			table.remove( C_PC.SysTray, k )
			local di = C_PC.DST[ k ]
			if (di) then di:Remove() end
			table.remove( C_PC.DST, k )
			return
		end
	end
end

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_progload: UserMessage Hook: Loads up a program upon notification of the server
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_progload( um )
	if (!C_PRO) then C_PRO = {} end
	if (C_PRO.Exists) then
		C_PRO:DestroyWindow()
	end
	progname = um:ReadString()
	C_PRO = ProgramList[ progname ]
end
usermessage.Hook( "_pcmod_runprog", pc_progload )

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_progunload: UserMessage Hook: Unloads the current program upon notification of the server
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_progunload( )
	if (!C_PRO) then C_PRO = {} end
	if (C_PRO.Exists) then C_PRO:ClearWindow( PTC("window") ) end
	C_PRO = {}
	local pn = DInterface:GetPanel( "window" )
	if (pn) then pn:KillFocus() end
	pc_focusdesktop()
	// For some reason, desktop icons stop working after this so we rebuild them.
	pc_builddesktopicons()
end
usermessage.Hook( "_pcmod_killprog", pc_progunload )

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_progwindow: UserMessage Hook: Pushes the window name through to the program, and rebuilds the window
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_progwindow( um )
	if (!C_PRO) then C_PRO = {} end
	if (!C_PRO.Exists) then return end
	winname = um:ReadString()
	C_PRO:ClearWindow( PTC("window") )
	C_PRO:BuildWindow( winname, PTC("window"), DInterface["d_prop_l" .. PTC("window")] )
	pc_focusprogram()
end
usermessage.Hook( "_pcmod_progwindow", pc_progwindow )

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_focusprogram: Brings focus to the program window
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_focusprogram()
	local window = DInterface:GetPanel( "window" )
	if (window) then window:RequestFocus() end
	// Msg( "Focus switched to program.\n" )
end

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_focusprogram: Brings focus to the desktop
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_focusdesktop()
	local window = DInterface:GetPanel( "desktop" )
	if (window) then window:RequestFocus() end
	// Msg( "Focus switched to desktop.\n" )
end

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_focusstart: Brings focus to the start menu
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_focusstart()
	local window = PCMod.StartMenu
	if (window) then window:RequestFocus() end
	// Msg( "Focus switched to start menu.\n" )
end

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_focusplist: Brings focus to the program list
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_focusplist()
	local window = PCMod.ProgramList
	if (window) then window:RequestFocus() end
	// Msg( "Focus switched to program list.\n" )
end